<?php
namespace app\manage\controller;
use fast\Tree;
use think\Log;

class AuthRule extends Base
{
    public function index()
    {

        $model = model('AuthRule');
        // 必须将结果集转换为数组
        $ruleList = collection($model->order('listorder', 'desc')->select())->toArray();


        Tree::instance()->init($ruleList);
        $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');

        return $this->fetch('', [
            'authRules' => $this->rulelist,
            'ruledata' => $this->rulelist,
        ]);
    }

    public function edit()
    {
        $id = input('id');
        $authRuleModel = model('AuthRule');
        $rule = $authRuleModel->getInfoByMap(['id'=>$id],'', '', false); // 单条信息

        return self::showJsonReturnCodeData('1001',$rule,'');

    }


    public function save()
    {
        if(request()->isAjax()){
            try{
                $authRuleModel = model('AuthRule');
                $id = input('post.id');
                $save_data['title'] = input('post.title');
                $save_data['name'] = input('post.name');
                $save_data['parent_id'] = input('post.parent_id');
                $save_data['icon'] = input('post.icon');
                $save_data['status'] = input('post.status');

                if(isset($id) && $id>0){
                    $save_data['id'] = $id;
                    // 修改时, 如何上级分类有改变, 需判断是否有子分类, 有的话禁止修改
                    $infosCount = $authRuleModel->countNum(['parent_id'=>$id],false); // 查询子分类数量
                    $rule = $authRuleModel->getInfoByMap(['id'=>$id],'', '', false);  // 查询单条信息
                    if($rule['parent_id'] != $save_data['parent_id']){
                        if($infosCount > 0 ){
                            return self::showReturnCode(1011,'该规则下有子规则,禁止修改上级用户组规则');
                        }
                    }
                }

                // 根据上级分类去修改等级
                if($save_data['parent_id']>0){
                    $level_info = $authRuleModel->getInfoByMap(['id'=>$save_data['parent_id']],'level','',false);
                    $level = $level_info['level'];
                    $save_data['level'] = $level+1;
                }else{
                    $save_data['level'] = 0;
                }

                $result = $this->editData(false,'AuthRule','AuthRule',$save_data);
                if($result['code'] == 1001){
                    if($id > 0){
                        $item = '修改';
                        $result['msg'] = '修改权限规则成功';
                    }else{
                        $item = '添加';
                        $result['msg'] = '添加权限规则成功';
                    }
                    $this->RecordSystemLog($this->uid,$this->behavior,$item.'权限规则【'.$save_data['title'].'】成功');
                }

                return $result;
            }catch (\Exception $e){
                Log::error($e->getMessage());
                return self::showReturnCode(1011,$e->getMessage());
            }
        }else{
            return self::showReturnCode(1002);
        }
    }


    public function delete()
    {
        if($this->request->isAjax()){
            $id = input('post.id');
            $model = $this->request->controller();
            $infoTemp = model($model)->getInfoByMap('','','',false);
            if(isset($infoTemp['parent_id'])){
                $infosCount = model($model)->countNum(['parent_id'=>$id],false); // 查询子分类数量
                if($infosCount > 0)  return $this->showReturnCode(1009,'该分类下有子分类,请先删除子分类');
            }
            $res = model($model)->where(['id'=>$id])->delete();
            if($res){
                $this->RecordSystemLog($this->uid,$this->behavior,'删除成功,操作ID: '.$id);
                return $this->showReturnCode(1001);
            }else{
                return $this->showReturnCode(1009,'删除失败');
            }
        }else{
            return $this->showReturnCode(1002);
        }
    }
}